package com.ldx.tvolatile;

import java.util.concurrent.TimeUnit;

/**
 * @author lidexin
 * @create 2021-10-17 22:04
 */
public class JMMDemo {
    //不加volatile程序进入死循环
    //加volatile可以保证可见性
    private volatile  static  int  num = 0;

    public static void main(String[] args) {

        new Thread(()-> {
            while(num ==0) {

            }
        }).start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        num = 1;
        System.out.println(num);
    }
}
